package com.ruoyi.web.controller.publish;

import java.util.List;

import com.ruoyi.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.publish.domain.ApprovalRecord;
import com.ruoyi.publish.service.IApprovalRecordService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 审批记录信息Controller
 *
 * @author confusefox
 * @date 2020-12-15
 */
@Controller
@RequestMapping("/publish/record")
public class ApprovalRecordController extends BaseController {
    private String prefix = "publish/report";

    @Autowired
    private IApprovalRecordService approvalRecordService;

    @RequiresPermissions("publish:record:view")
    @GetMapping()
    public String record() {
        return prefix + "/record";
    }

    /**
     * 查询审批记录信息列表
     */
    @RequiresPermissions("publish:record:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ApprovalRecord approvalRecord) {
        startPage();
        List<ApprovalRecord> list = approvalRecordService.selectApprovalRecordList(approvalRecord);
        return getDataTable(list);
    }

    /**
     * 导出审批记录信息列表
     */
    @RequiresPermissions("publish:record:export")
    @Log(title = "审批记录信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ApprovalRecord approvalRecord) {
        List<ApprovalRecord> list = approvalRecordService.selectApprovalRecordList(approvalRecord);
        ExcelUtil<ApprovalRecord> util = new ExcelUtil<ApprovalRecord>(ApprovalRecord.class);
        return util.exportExcel(list, "record");
    }

    /**
     * 新增审批记录信息
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存审批记录信息
     */
    @RequiresPermissions("publish:record:add")
    @Log(title = "审批记录信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ApprovalRecord approvalRecord) {
        return toAjax(approvalRecordService.insertApprovalRecord(approvalRecord));
    }

    /**
     * 修改审批记录信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        ApprovalRecord approvalRecord = approvalRecordService.selectApprovalRecordById(id);
        mmap.put("approvalRecord", approvalRecord);
        return prefix + "/edit";
    }

    /**
     * 修改审批记录信息
     */
    @GetMapping("/detail/{id},{module}")
    public String detail(@PathVariable("id") Long id,@PathVariable("module") int module, ModelMap mmap) {
        ApprovalRecord record = new ApprovalRecord();
        record.setRecordId(id);
        record.setSystemModule(module);
        List<ApprovalRecord> approvalRecordList = approvalRecordService.selectApprovalRecordList(record);
        mmap.put("approvalRecordList", approvalRecordList);
        return prefix + "/approvalhistory";
    }


    /**
     * 修改保存审批记录信息
     */
    @RequiresPermissions("publish:record:edit")
    @Log(title = "审批记录信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ApprovalRecord approvalRecord) {
        return toAjax(approvalRecordService.updateApprovalRecord(approvalRecord));
    }

    /**
     * 删除审批记录信息
     */
    @RequiresPermissions("publish:record:remove")
    @Log(title = "审批记录信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(approvalRecordService.deleteApprovalRecordByIds(ids));
    }


    // @RequiresPermissions("publish:record:edit")
    @Log(title = "审批信息", businessType = BusinessType.UPDATE)
    @PostMapping("/approvalReport")
    @ResponseBody
    public AjaxResult approvalReport(ApprovalRecord approvalRecord) {
        // 设置当前登录人为审批人
        approvalRecord.setApprovalUser(ShiroUtils.getSysUser().getUserId());
        approvalRecord.setApprovalUserName(ShiroUtils.getSysUser().getUserName());
        return approvalRecordService.approvalReport(approvalRecord);
    }

    @Log(title = "添加送审人员", businessType = BusinessType.UPDATE)
    @PostMapping("/addApprovalUser")
    @ResponseBody
    public AjaxResult addApprovalUser(ApprovalRecord approvalRecord) {
        // 设置当前登录人为送审人员
        approvalRecord.setApprovalUser(ShiroUtils.getSysUser().getUserId());
        approvalRecord.setApprovalUserName(ShiroUtils.getSysUser().getUserName());
        return approvalRecordService.addApprovalStaff(approvalRecord);
    }
}
